package dao

import (
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/usersteward"
	"yundian/internel/pkg/dingo"
	"yundian/internel/pkg/dingo/global"
)

type UsersStaffDao struct{}

func NewUsersStaffDao() *UsersStaffDao {
	return &UsersStaffDao{}
}

// SetUserInfo 设置员工信息
func (dao *UsersStaffDao) SetUserInfo(param *request.AppSetUserInfoReq) *dingo.ErrorWrap {
	dbInfo := map[string]interface{}{}
	if param.Avatar != "" {
		dbInfo["icon"] = param.Avatar
	}
	if param.Name != "" {
		dbInfo["name"] = param.Name
	}
	if param.TencentOpenid != "" {
		dbInfo["tencent_openid"] = param.TencentOpenid
	}
	if param.UpdateTime != "" {
		dbInfo["update_time"] = param.UpdateTime
	}

	db := global.Orm.Table("users_staff").Where("id=?", param.LoginUserId)
	db.Updates(dbInfo)

	return dingo.ErrorIf(db.Error, true)
}

// SetUserOnlineStatus 设置员工在线状态
func (dao *UsersStaffDao) SetUserOnlineStatus(param *request.SetUserOnlineStatusReq) *dingo.ErrorWrap {
	db := global.Orm.Table("places_staff")
	db.Where("staff_id=? AND place_id=? AND status IN (1,2)", param.LoginUserId, param.PlaceId)
	db.Update("status", param.Status)

	return dingo.ErrorIf(db.Error, true)
}

// GetStaffUserPlacesListById 查询员工已关联网点
func (dao *UsersStaffDao) GetStaffUserPlacesListById(id int) *dingo.ErrorWrap {
	var dbList []*request.StewardAppGetPlacesRes
	db := global.Orm.Table("places_staff ps").Select("ps.place_id,ps.status,p.place_name")
	db.Joins("LEFT JOIN places p ON ps.place_id=p.id")
	db.Where("ps.staff_id=? AND ps.status IN (1,2)", id)

	db.Find(&dbList)
	return dingo.ErrorIf(db.Error, dbList)
}

// GetStaffUserPlacesInfoById 查询员工已关联网点
func (dao *UsersStaffDao) GetStaffUserPlacesInfoById(id, placeId int) *dingo.ErrorWrap {
	dbInfo := &request.StewardAppGetPlacesRes{}
	db := global.Orm.Table("places_staff ps").Select("ps.place_id,ps.status,p.place_name")
	db.Joins("LEFT JOIN places p ON ps.place_id=p.id")
	db.Where("p.id=? AND ps.staff_id=? AND ps.status IN (1,2)", placeId, id)

	db.Find(&dbInfo)
	return dingo.ErrorIf(db.Error, dbInfo)
}

// SetStaffUserPhone 设置员工帐号手机号
func (dao *UsersStaffDao) SetStaffUserPhone(param *request.AppSmsCodeReq) *dingo.ErrorWrap {
	db := global.Orm.Table("users_staff").Where("id=?", param.LoginUserId).Update("phone", param.Phone)
	if db.Error == nil {
		global.Orm.Table("sms_code").Where("phone=? AND code=?", param.Phone, param.Code).Update("status", 1)
	}
	return dingo.ErrorIf(db.Error, true)
}

func (dao *UsersStaffDao) GetStaffUserOpenidById(id int) *dingo.ErrorWrap {
	dbInfo := &request.GetStaffUserOpenidByIdRes{}
	fields := `id,tencent_openid,DATE_FORMAT(update_time,'%Y/%m/%d %T')update_time`
	db := global.Orm.Table("users_staff").Select(fields)
	db.Where("id=?", id).Find(&dbInfo)

	return dingo.ErrorIf(db.Error, dbInfo)
}

// GetStaffUserInfoById 查询员工信息
func (dao *UsersStaffDao) GetStaffUserInfoById(id int) *usersteward.StUserStaffBasic {
	dbInfo := &usersteward.StUserStaffBasic{}
	global.Orm.Select("id,role_name,name,phone,icon,sex,message_types").Where("id=?", id).Find(&dbInfo)
	return dbInfo
}

// GetStaffUserInfoAllById 查询员工信息
func (dao *UsersStaffDao) GetStaffUserInfoAllById(id int) *usersteward.StUserStaff {
	dbInfo := &usersteward.StUserStaff{}
	global.Orm.Where("id=?", id).Find(&dbInfo)
	return dbInfo
}

// UpdateUserPassword 更新用户密码
func (dao *UsersStaffDao) UpdateUserPassword(id int, password string) *dingo.ErrorWrap {
	db := global.Orm.Exec("update users_staff set password = ? where id = ?", password, id)
	return dingo.ErrorIf(db.Error, true)
}
